<template>
  <div class="tasks-page">
    <el-row :gutter="20">
      <!-- 任务配置 -->
      <el-col :xs="24" :lg="10">
        <el-card>
          <template #header>
            <div class="card-header">
              <span class="title">🎯 成长任务配置</span>
              <el-button type="primary" size="small" @click="handleAddTask">
                <el-icon><Plus /></el-icon>
                新增任务
              </el-button>
            </div>
          </template>

          <el-collapse v-model="activeTaskType">
            <el-collapse-item title="新手任务" name="newbie">
              <div class="task-list">
                <div v-for="task in newbieTasks" :key="task.id" class="task-item">
                  <div class="task-info">
                    <div class="task-name">
                      <el-icon><DocumentChecked /></el-icon>
                      {{ task.name }}
                    </div>
                    <div class="task-reward">
                      奖励: 
                      <el-tag type="warning" size="small">{{ task.points }}积分</el-tag>
                      <el-tag v-if="task.coupon" type="success" size="small">{{ task.coupon }}</el-tag>
                    </div>
                  </div>
                  <div class="task-actions">
                    <el-switch v-model="task.enabled" size="small" />
                    <el-button link type="primary" size="small" @click="handleEditTask(task)">编辑</el-button>
                  </div>
                </div>
              </div>
            </el-collapse-item>

            <el-collapse-item title="每日任务" name="daily">
              <div class="task-list">
                <div v-for="task in dailyTasks" :key="task.id" class="task-item">
                  <div class="task-info">
                    <div class="task-name">
                      <el-icon><DocumentChecked /></el-icon>
                      {{ task.name }}
                    </div>
                    <div class="task-reward">
                      奖励: <el-tag type="warning" size="small">{{ task.points }}积分</el-tag>
                    </div>
                  </div>
                  <div class="task-actions">
                    <el-switch v-model="task.enabled" size="small" />
                    <el-button link type="primary" size="small" @click="handleEditTask(task)">编辑</el-button>
                  </div>
                </div>
              </div>
            </el-collapse-item>

            <el-collapse-item title="成就任务" name="achievement">
              <div class="task-list">
                <div v-for="task in achievementTasks" :key="task.id" class="task-item">
                  <div class="task-info">
                    <div class="task-name">
                      <el-icon><Trophy /></el-icon>
                      {{ task.name }}
                    </div>
                    <div class="task-reward">
                      奖励: <el-tag type="danger" size="small">{{ task.points }}积分</el-tag>
                    </div>
                  </div>
                  <div class="task-actions">
                    <el-switch v-model="task.enabled" size="small" />
                    <el-button link type="primary" size="small" @click="handleEditTask(task)">编辑</el-button>
                  </div>
                </div>
              </div>
            </el-collapse-item>
          </el-collapse>
        </el-card>
      </el-col>

      <!-- 任务完成情况 -->
      <el-col :xs="24" :lg="14">
        <el-card>
          <template #header>
            <span class="title">📈 任务完成统计</span>
          </template>

          <!-- 今日任务完成 -->
          <div class="today-stats">
            <el-row :gutter="20">
              <el-col :span="6">
                <el-statistic title="今日完成" :value="456" />
              </el-col>
              <el-col :span="6">
                <el-statistic title="参与人数" :value="234" />
              </el-col>
              <el-col :span="6">
                <el-statistic title="完成率" :value="78.5" suffix="%" />
              </el-col>
              <el-col :span="6">
                <el-statistic title="发放积分" :value="2890" />
              </el-col>
            </el-row>
          </div>

          <!-- 任务完成排行 -->
          <div class="task-ranking">
            <h4>任务完成排行榜</h4>
            <el-table :data="taskRanking" stripe>
              <el-table-column type="index" label="排名" width="60">
                <template #default="{ $index }">
                  <el-tag v-if="$index < 3" :type="getRankTag($index)">
                    {{ $index + 1 }}
                  </el-tag>
                  <span v-else>{{ $index + 1 }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="memberName" label="会员姓名" width="150" />
              <el-table-column prop="completedCount" label="完成任务" width="100">
                <template #default="{ row }">
                  <el-tag>{{ row.completedCount }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="totalPoints" label="获得积分" width="100">
                <template #default="{ row }">
                  <span style="color: #f56c6c; font-weight: bold;">{{ row.totalPoints }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="level" label="等级" width="120" />
              <el-table-column prop="progress" label="进度">
                <template #default="{ row }">
                  <el-progress :percentage="row.progress" />
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 任务完成记录 -->
    <el-card style="margin-top: 20px;">
      <template #header>
        <span>📋 任务完成记录</span>
      </template>
      <el-table :data="completionRecords" border stripe>
        <el-table-column prop="memberName" label="会员姓名" width="150" />
        <el-table-column prop="taskName" label="任务名称" width="200" />
        <el-table-column prop="taskType" label="任务类型" width="120">
          <template #default="{ row }">
            {{ getTaskTypeName(row.taskType) }}
          </template>
        </el-table-column>
        <el-table-column prop="completeTime" label="完成时间" width="160" />
        <el-table-column prop="points" label="获得积分" width="100">
          <template #default="{ row }">
            <el-tag type="danger">+{{ row.points }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 'completed' ? 'success' : 'info'">
              {{ row.status === 'completed' ? '已完成' : '进行中' }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 任务编辑对话框 -->
    <el-dialog v-model="taskDialogVisible" :title="taskDialogTitle" width="600px">
      <el-form :model="taskForm" label-width="100px">
        <el-form-item label="任务名称">
          <el-input v-model="taskForm.name" />
        </el-form-item>
        <el-form-item label="任务类型">
          <el-select v-model="taskForm.type">
            <el-option label="新手任务" value="newbie" />
            <el-option label="每日任务" value="daily" />
            <el-option label="成就任务" value="achievement" />
          </el-select>
        </el-form-item>
        <el-form-item label="任务要求">
          <el-input v-model="taskForm.requirement" type="textarea" :rows="3" />
        </el-form-item>
        <el-form-item label="奖励积分">
          <el-input-number v-model="taskForm.points" :min="0" />
        </el-form-item>
        <el-form-item label="奖励券">
          <el-select v-model="taskForm.coupon" clearable>
            <el-option label="无" value="" />
            <el-option label="10元券" value="10元券" />
            <el-option label="20元券" value="20元券" />
          </el-select>
        </el-form-item>
        <el-form-item label="启用状态">
          <el-switch v-model="taskForm.enabled" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="taskDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleTaskSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus, DocumentChecked, Trophy } from '@element-plus/icons-vue'

const activeTaskType = ref(['newbie', 'daily', 'achievement'])

// 新手任务
const newbieTasks = ref([
  { id: 1, name: '完善个人资料', points: 50, coupon: '10元券', enabled: true },
  { id: 2, name: '首次购物', points: 100, coupon: '20元券', enabled: true },
  { id: 3, name: '关注公众号', points: 20, coupon: '', enabled: true },
  { id: 4, name: '首次评价', points: 30, coupon: '', enabled: true }
])

// 每日任务
const dailyTasks = ref([
  { id: 1, name: '每日签到', points: 5, enabled: true },
  { id: 2, name: '浏览商品', points: 2, enabled: true },
  { id: 3, name: '分享商品', points: 3, enabled: true }
])

// 成就任务
const achievementTasks = ref([
  { id: 1, name: '累计消费满1万', points: 500, enabled: true },
  { id: 2, name: '累计消费满5万', points: 2000, enabled: true },
  { id: 3, name: '累计消费满10万', points: 5000, enabled: true },
  { id: 4, name: '推荐10位好友', points: 1000, enabled: true }
])

// 任务排行
const taskRanking = ref([
  { memberName: '张三', completedCount: 25, totalPoints: 1250, level: '钻石会员', progress: 95 },
  { memberName: '李四', completedCount: 20, totalPoints: 980, level: '金卡会员', progress: 82 },
  { memberName: '王五', completedCount: 18, totalPoints: 890, level: '银卡会员', progress: 75 }
])

// 完成记录
const completionRecords = ref([
  { memberName: '张三', taskName: '每日签到', taskType: 'daily', completeTime: '2024-01-20 09:30', points: 5, status: 'completed' },
  { memberName: '李四', taskName: '首次购物', taskType: 'newbie', completeTime: '2024-01-20 14:20', points: 100, status: 'completed' }
])

const taskDialogVisible = ref(false)
const taskDialogTitle = ref('新增任务')

const taskForm = reactive({
  name: '',
  type: 'daily',
  requirement: '',
  points: 0,
  coupon: '',
  enabled: true
})

const getRankTag = (index) => {
  const tags = ['danger', 'warning', 'success']
  return tags[index]
}

const getTaskTypeName = (type) => {
  const map = { newbie: '新手任务', daily: '每日任务', achievement: '成就任务' }
  return map[type] || type
}

const handleAddTask = () => {
  taskDialogTitle.value = '新增任务'
  taskDialogVisible.value = true
}

const handleEditTask = (task) => {
  taskDialogTitle.value = '编辑任务'
  Object.assign(taskForm, task)
  taskDialogVisible.value = true
}

const handleTaskSubmit = () => {
  ElMessage.success('保存成功')
  taskDialogVisible.value = false
}
</script>

<style scoped>
.tasks-page {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.task-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.task-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  background: #f5f7fa;
  border-radius: 8px;
  transition: all 0.3s;
}

.task-item:hover {
  background: #ecf5ff;
  transform: translateX(5px);
}

.task-info {
  flex: 1;
}

.task-name {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: bold;
  margin-bottom: 8px;
}

.task-reward {
  font-size: 13px;
  color: #606266;
  display: flex;
  align-items: center;
  gap: 8px;
}

.task-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.today-stats {
  margin-bottom: 30px;
  padding: 20px;
  background: #f5f7fa;
  border-radius: 8px;
}

.task-ranking {
  margin-top: 20px;
}

.task-ranking h4 {
  margin: 0 0 15px 0;
}
</style>

